//
//  MeController.swift
//  BodyBuilding
//
//  Created by franze on 2017/9/30.
//  Copyright © 2017年 franze. All rights reserved.
//

import UIKit

class MeController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    var tableView:UITableView!
    var list:NSArray?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        viewConfig()
        
        DispatchQueue.global(qos: .default).async {
            self.getList()
        }
    }
    
    func viewConfig(){
        view.backgroundColor = UIColor.white
        
        let navBar = CustomNavBar(frame: CGRect(origin: .zero, size: CGSize(width: WIDTH, height: 64)))
        navBar.initialize(text: "训练")
        navBar.backBtn.isHidden = true
        view.addSubview(navBar)
        
        tableView = UITableView(frame: CGRect(x: 0, y: 64, width: WIDTH, height: HEIGHT-113))
        tableView.register(ClubCell.self, forCellReuseIdentifier: "cell")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.isScrollEnabled = false
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.backgroundColor = view.backgroundColor
        tableView.tableFooterView = UIView()
        view.addSubview(tableView)
        
    }
    
    func getList(){
        let parameter:NSDictionary = ["SessionID":LoginModel.sessionID,"page":"1","mod":"training"]
        Networking.shared.getTrainingList(parameter: parameter) { (json) in
            self.list = json
            self.tableView.reloadData()
        }
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return list?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ClubCell
        let dict = list![indexPath.row] as! NSDictionary
        
        cell.imageView?.sd_setImage(with: URL(string: BB_IMAGEURLPRE+(dict.value(forKey: "thumb") as! String)), placeholderImage: UIImage(named:"placeholder"))
        cell.textLabel?.text = dict.value(forKey: "train_name") as? String
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let controller = MeDetailController()
        controller.infoDict = list![indexPath.row] as! NSDictionary
        
        hidesBottomBarWhenPushed = true
        _ = navigationController?.pushViewController(controller, animated: true)
        hidesBottomBarWhenPushed = false
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return (HEIGHT-69)/3
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
